<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工号" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入工号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总分" prop="total">
        <el-input
            v-model="queryParams.total"
            placeholder="请输入总分"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="等级" prop="classification">
        <el-select
            v-model="queryParams.classification"
            placeholder="请输入等级"
            clearable
            style="width: 240px"
        >
          <el-option
              v-for="item in classification"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable @keyup.enter.native="handleQuery">
          <el-option
              v-for="dict in dict.type.dept_abbreviation"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="userClass">
        <el-select
            v-model="queryParams.userClass"
            placeholder="用户类型"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        >
          <el-option
              v-for="dict in dict.type.sys_user_class"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
        <el-select
            v-model="queryParams.companyId"
            placeholder="所属公司"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        >
          <el-option
              v-for="dict in dict.type.sys_user_company"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['quality:userClassification:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['quality:userClassification:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['quality:userClassification:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['quality:userClassification:edit']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :disabled="multiple"
            @click="handleExport"
            v-hasPermi="['quality:userClassification:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleAllExport"
            v-hasPermi="['quality:userClassification:export']"
        >全部导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userClassificationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" v-if="true" width="60"/>
      <el-table-column label="工号" align="center" prop="userName" />
      <el-table-column label="姓名" align="center" key="nickName" prop="classDataUser.nickName"/>
      <el-table-column label="部门" align="center" key="deptName" prop="classDataUser.deptId" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dept_abbreviation" :value="scope.row.classDataUser.deptId"/>
        </template>
      </el-table-column>
      <el-table-column label="公司" align="center" key="companyId" prop="classDataUser.companyId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_company" :value="scope.row.classDataUser.companyId"/>
        </template>
      </el-table-column>
      <el-table-column label="理论" align="center" prop="theory" />
      <el-table-column label="实操" align="center" prop="practice">
        <template slot-scope="scope">
          <p>{{scope.row.practice}}<el-tag size="small" v-if="scope.row.flag === '1'" class="ml-2" type="danger">补</el-tag></p>
        </template>
      </el-table-column>
      <el-table-column label="考核" align="center" prop="examine" />
      <el-table-column label="总分" align="center" prop="total" />
      <el-table-column label="等级" align="center" prop="classification" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click.stop="handlePreView(scope.row)"
              v-hasPermi="['quality:userClassification:export']"
          >预览</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleUpdate(scope.row)"
              v-hasPermi="['quality:userClassification:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click.stop="handleDelete(scope.row)"
              v-hasPermi="['quality:userClassification:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改用户分级对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="理论" prop="theory">
          <el-input v-model="form.theory" placeholder="请输入理论分" />
        </el-form-item>
        <el-form-item label="考核" prop="examine">
          <el-input v-model="form.examine" placeholder="请输入考核分" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <div>
      <welder-card
          :visible.sync="preview"
          :user = user
      />
    </div>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body style="text-align: center">
      <el-upload
          ref="upload"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserClassification, getUserClassification, delUserClassification, addUserClassification, updateUserClassification } from "@/api/quality/userClassification";
import WelderCard from '@/views/quality/common/WelderCard.vue'
import { getUser } from '@/api/system/user'
import { getToken } from '@/utils/auth'

export default {
  name: "UserClassification",
  components: { WelderCard },
  dicts: ['sys_user_company', 'sys_user_class', 'dept_abbreviation'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //选中用户
      user: {},
      classification:[
        { value: 'A', label: 'A'},
        { value: 'B', label: 'B'},
        { value: 'C', label: 'C'},
     ],
      //通报预览开关
      preview:false,
      // 用户分级表格数据
      userClassificationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //电焊工岗位ID
      heatingId: 7 ,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        total: undefined,
        classification: undefined,
        deptId: undefined,
        userClass: undefined,
        companyId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "id不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "工号不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "总分不能为空", trigger: "blur" }
        ],
        classification: [
          { required: true, message: "等级不能为空", trigger: "change" }
        ],
      },
      // 不合格品导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/quality/userClassification/importData"
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户分级列表 */
    getList() {
      this.loading = true;
      listUserClassification(this.queryParams).then(response => {
        this.userClassificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userName: undefined,
        total: undefined,
        classification: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户分级";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUserClassification(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改用户分级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUserClassification(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUserClassification(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户分级编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delUserClassification(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 全部导出按钮操作 */
    handleAllExport() {
      this.download('quality/userClassification/exportAll', {
        ...this.queryParams
      }, `userClassification_${new Date().getTime()}.xlsx`)
    },
    /** 选择项导出按钮操作 */
    handleExport(row) {
      const ids = row.id || this.ids;
      this.download('quality/userClassification/exportSelect/'+ids, {
        ...this.queryParams
      }, `userClassification_${new Date().getTime()}.xlsx`)
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('quality/userClassification/importTemplate', {
      }, `userClassification_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 点击列表预览通报内容*/
    handlePreView(row) {
      this.user = { ...row.classDataUser };
      this.user.total = Number(row.total);
      this.preview = true;
      this.user.classification = row.classification;
    },
  }
};
</script>
